//
//  SOHtmlWebViewController.m
//  so
//
//  Created by steven on 16/11/01.
//  Copyright © 2016年 steven. All rights reserved.
//

#import "JHHtmlWebViewController.h"
#import "WebViewJavascriptBridge.h"

#import <cmbkeyboard/CMBWebKeyboard.h>
#import <cmbkeyboard/NSString+Additions.h>

@interface JHHtmlWebViewController()<UIGestureRecognizerDelegate>
@property WebViewJavascriptBridge* bridge;
@end

@implementation JHHtmlWebViewController



-(void)viewDidLoad{
    [super viewDidLoad];
    self.navigationItem.title = @"收银台";
    
}
- (void)viewWillAppear:(BOOL)animated
{
    [self viewinitial];
    
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[CMBWebKeyboard shareInstance] hideKeyboard];
}
- (void)viewinitial
{
    
    if (_bridge) {
        return;
    }
    self.automaticallyAdjustsScrollViewInsets = NO;
    _webView = [[UIWebView alloc]initWithFrame:self.view.bounds];
    [self.view addSubview:_webView];
    
    [WebViewJavascriptBridge enableLogging];
    
    _bridge = [WebViewJavascriptBridge bridgeForWebView:_webView];
    [_bridge setWebViewDelegate:self];
    [_bridge registerHandler:@"wxPay" handler:^(id data, WVJBResponseCallback responseCallback) {
        [self wxPay:data];
    }];
    [_bridge registerHandler:@"wxPayFinish" handler:^(id data, WVJBResponseCallback responseCallback) {
        [self payCompleted];
    }];
    
    [[CMBWebKeyboard shareInstance] hideKeyboard];
    
    
    
    
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.url]];
    [_webView loadRequest:request];
    
    // 这里的mainBundle指的是APP的sandbox
    NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"JHPAY_SOURSE" ofType:@"bundle"];
    // SDK对应的bundle
    NSBundle *bundle = [NSBundle bundleWithPath:bundlePath];
    NSString *thePath = [bundle pathForResource:@"cancel" ofType:@"png"];
    UIImage *image = [UIImage imageWithContentsOfFile:thePath];
    
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 32)];
    [button setImage:image forState:UIControlStateNormal];
    [button setImageEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 0)];
    [button addTarget:self action:@selector(leftButtonOnClick:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.leftBarButtonItem = barButtonItem;
}
#pragma mark - NJKWebViewProgressDelegate

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    NSString *urlString = [[request URL] absoluteString];
    NSLog(@"url -----> %@",urlString);
    
    if ([request.URL.host isCaseInsensitiveEqualToString:@"cmbls"]) {
        CMBWebKeyboard *secKeyboard = [CMBWebKeyboard shareInstance];
        [secKeyboard showKeyboardWithRequest:request];
        secKeyboard.webView = _webView;
        
        UITapGestureRecognizer* myTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
        [self.view addGestureRecognizer:myTap]; //这个可以加到任何控件上,比如你只想响应WebView，我正好填满整个屏幕
        myTap.delegate = self;
        myTap.cancelsTouchesInView = NO;
        return NO;
    } else if ([request.URL.absoluteString containsString:@"/ja/common/v1/pay/notify/yiwangtong/return"]) {
        [self payCompleted];
        return NO;
    }else if ([urlString containsString:@"/ja/memberbao/v1/pay/return"]) {
        [self payCompleted];
        return NO;
    }
    
    return YES;
}
-(void)handleSingleTap:(UITapGestureRecognizer *)sender{
    [[CMBWebKeyboard shareInstance] hideKeyboard];
    
}

- (void)wxPay:(id)data
{
    NSDictionary *dic = (NSDictionary *)data;
    if (self.delegate  && [self.delegate respondsToSelector:@selector(wxPay:)]) {
        [self.delegate wxPay:dic];
    }
}
- (void)wxPayFinish
{
    [self payCompleted];
}

- (void)payCompleted
{
    if (self.delegate  && [self.delegate respondsToSelector:@selector(payCompleted)]) {
        [self.delegate payCompleted];
    }
}
-(void)leftButtonOnClick:(id)sender
{
    [self payCompleted];
    
}
- (void)dealloc
{
    [[CMBWebKeyboard shareInstance] hideKeyboard];
}
//-(void)webViewDidFinishLoad:(UIWebView *)webView {
//    self.navigationItem.title = [_webView stringByEvaluatingJavaScriptFromString:@"document.title"];
//}
@end
